---
title: "Cleaning DPI PR/SMP coding"
---

# Load

```{r}
# load packages
  source("helper-packages.R")

# load dpi dataset
  dpi_raw <- 
    import("../raw-data/x-dpi/DPI2020_stata13.dta")
```

Question:

HOUSESYS
Which electoral rule (proportional representation or plurality) governs the election of the majority of
House seats?
This is coded 1 if most seats are Plurality, zero if most seats are Proportional. In cases where the majority of
legislators are appointed or indirectly elected, HOUSESYS is coded Indirect.

# Clean DIP

```{r}
# clean
  dpi_clean <- 
    dpi_raw %>% 
    select(
      countryname, 
      year, 
      housesys) %>%
    filter(countryname != "Turk Cyprus") %>% # we're only looking at the republic of cyprus
    mutate(
      dpi_housesys_pr = 
        case_when(
          housesys == 0 ~ 1,
          housesys == 0.5 ~ 0, # the codebook does not define 0.5 but assume half-half; default to plurality in this case
          housesys == 1 ~ 0,
          TRUE ~ NA_real_),
      dpi_housesys_plurality = 
        case_when(
          housesys == 0 ~ 0,
          housesys == 0.5 ~ 1, # the codebook does not define 0.5 but assume half-half; default to plurality in this case
          housesys == 1 ~ 1,
          TRUE ~ NA_real_),
      dpi_country_common = 
        dplyr::recode(
          countryname,
          "Cent. Af. Rep." = "Central African Republic",
          "GDR" = "East Germany",
          "PRC" = "China",
          "Dom. Rep." = "Dominican Republic",
          "ROK" = "Republic of Korea",
          "PRK" = "North Korea",
          "S. Africa" = "South Africa",
          "P. N. Guinea" = "Papua New Guinea"),
      dpi_country_common = countryname(dpi_country_common)) %>% 
      select(
        dpi_country_common,
        dpi_year = year,
        dpi_housesys_pr,
        dpi_housesys_plurality) %>% 
      filter(!is.na(dpi_housesys_pr) | !is.na(dpi_housesys_plurality))
```

# Save data

```{r}
  saveRDS(dpi_clean, "../cleaned-data/x-11-smp-pr.rds")
```